package swings.panels;

import entity.CommonParam;
import swings.frames.OperateSeqFrame;
import swings.frames.OperateSignFrame;
import swings.labels.AddParamLabel;
import swings.labels.HeaderLabel;
import swings.labels.LimpidLabel;
import swings.labels.ParamLabel;
import utils.ImageUtils;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.*;

/**
 * @version V1.0
 * @Title:
 * @Package
 * @Description:
 * @author: runoob
 * @date:
 */
public class TemplatePanel extends CommonPanel{

    private LimpidLabel bgLabel = new LimpidLabel(ImageUtils.common,0, 0, WorkPanel.width-10, WorkPanel.height/2-30);
    private ParamLabel dataLabel = new ParamLabel(40, 0, "data", "根据Param自动生成", "数据", this);
    private ParamLabel timestampLabel = new ParamLabel(70, 1, "timeStamp", "自动生成", "时间戳", this);
    private ParamLabel seqLabel = new ParamLabel(100, 2, "seq", "自增序列", this);
    private ParamLabel signLabel = new ParamLabel(130, 3, "sign", "参数签名", this);
    private HeaderLabel headerLabel = new HeaderLabel(5, "KEY", "OPERATE", "DESCRIPTION");
    private AddParamLabel addParamLabel = new AddParamLabel(165, this);
    private int y = 160;
    private int symbol = 4;
    public Map<Integer, ParamLabel> paramMap = new HashMap<>();

    public TemplatePanel() throws IOException {
        paramMap.put(0, dataLabel);
        paramMap.put(1, timestampLabel);
        paramMap.put(2, seqLabel);
        paramMap.put(3, signLabel);
        dataLabel.setValueUneditable();
        dataLabel.setDescriptionUneditable();
        timestampLabel.setValueUneditable();
        timestampLabel.setDescriptionUneditable();
        seqLabel.setValueUneditable();
        seqLabel.setDescriptionUneditable();
        signLabel.setDescriptionUneditable();
        seqLabel.operate.addActionListener(new operateSeq());
        signLabel.operate.addActionListener(new operateSign());
        this.add(dataLabel);
        this.add(timestampLabel);
        this.add(seqLabel);
        this.add(signLabel);
        this.add(headerLabel);
        this.add(addParamLabel);
        this.setBounds(5,81,WorkPanel.width-10,WorkPanel.height/2-80);
        this.add(bgLabel);
        this.setBorder(null);
    }

    public void newParamLabel() throws IOException {
        ParamLabel cache = new ParamLabel(y, symbol, "", "", "", this);
        paramMap.put(symbol, cache);
        this.remove(bgLabel);
        this.add(cache);
        this.add(bgLabel);
        this.invalidate();
        this.validate();
        this.repaint();
        y += 30;
        symbol++;
    }

    /**
     * hold监听器
     */
    private class operateSeq implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            new OperateSeqFrame(seqLabel);
        }
    }

    /**
     * hold监听器
     */
    private class operateSign implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                List<String> dataList = new ArrayList<>();
                for (Integer i : paramMap.keySet()){
                    dataList.add(paramMap.get(i).keyField.getText());
                }
                new OperateSignFrame(signLabel, dataList);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }

    @Override
    public void removeParamLabel(int symbol){
        y = 40;
        this.removeAll();
        paramMap.remove(symbol);
        Set<Map.Entry<Integer, ParamLabel>> entries = paramMap.entrySet();
        for (Map.Entry<Integer, ParamLabel> entry : entries) {
            ParamLabel cache = entry.getValue();
            cache.setY(y);
            this.add(cache);
            y += 30;
        }
        addParamLabel = new AddParamLabel(y+5, this);
        this.add(headerLabel);
        this.add(addParamLabel);
        this.add(bgLabel);
        refresh();
    }

    public ArrayList<CommonParam> getData(){
        ArrayList<CommonParam> result = new ArrayList<>();
        Set<Map.Entry<Integer, ParamLabel>> entries = paramMap.entrySet();
        for (Map.Entry<Integer, ParamLabel> entry : entries) {
            ParamLabel cache = entry.getValue();
            if (cache.isChosen){
                CommonParam data = new CommonParam();
                data.setKey(cache.keyField.getText());
                data.setValue(cache.valueField.getText());
                data.setDescription(cache.descriptionField.getText());
                result.add(data);
            }
        }
        return result;
    }

    public void setData(ArrayList<CommonParam> data) throws IOException {
        this.removeAll();
        this.add(dataLabel);
        this.add(timestampLabel);
        this.add(seqLabel);
        this.add(signLabel);
        this.add(headerLabel);
        this.add(addParamLabel);
        paramMap.clear();
        paramMap.put(0, dataLabel);
        paramMap.put(1, timestampLabel);
        paramMap.put(2, seqLabel);
        paramMap.put(3, signLabel);
        if (null != data && data.size()>3){
            int j = 0;
            for (int i=0;i<data.size();i++){
                String key = data.get(i).getKey();
                String value = data.get(i).getValue();
                String description = data.get(i).getDescription();
                if (key.toLowerCase().equals("data")){
                    dataLabel.keyField.setText(key);
                    dataLabel.valueField.setText(value);
                    dataLabel.descriptionField.setText(description);
                }else if (key.toLowerCase().equals("timestamp")){
                    timestampLabel.keyField.setText(key);
                    timestampLabel.valueField.setText(value);
                    timestampLabel.descriptionField.setText(description);
                }else if (key.toLowerCase().contains("seq")){
                    seqLabel.keyField.setText(key);
                    seqLabel.valueField.setText(value);
                    seqLabel.descriptionField.setText(description);
                }else if (key.toLowerCase().contains("sig")){
                    signLabel.keyField.setText(key);
                    signLabel.valueField.setText(value);
                    signLabel.descriptionField.setText(description);
                }else {
                    ParamLabel cache = new ParamLabel(160 + j*30, i, key, value, description,this);
                    this.add(cache);
                    paramMap.put(symbol, cache);
                    symbol++;
                    j++;
                }
            }
            this.remove(bgLabel);
            this.remove(addParamLabel);
            y = 160 + j*30;
            addParamLabel = new AddParamLabel(y + 5, this);
            this.add(addParamLabel);
        }else {
            dataLabel.valueField.setText("根据Param自动生成");
            dataLabel.descriptionField.setText("数据");
            timestampLabel.valueField.setText("自动生成");
            timestampLabel.descriptionField.setText("时间戳");
            seqLabel.valueField.setText("");
            seqLabel.descriptionField.setText("自增序列");
            signLabel.valueField.setText("");
            signLabel.descriptionField.setText("参数签名");
            this.add(dataLabel);
            this.add(timestampLabel);
            this.add(seqLabel);
            this.add(signLabel);
            this.add(headerLabel);
            this.add(addParamLabel);
            this.setBounds(5,81,WorkPanel.width-10,WorkPanel.height/2-80);
        }
        this.add(bgLabel);
        refresh();
    }

    public void refresh(){
        this.invalidate();
        this.validate();
        this.repaint();
    }

}
